package com.ruoyi.develop.lvBuy.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import javax.annotation.Resource;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.develop.goodsSalebuy.domain.GoodsSalebuy;
import com.ruoyi.develop.projectBaseInfo.domain.ProjectBaseInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.lvBuy.mapper.LvBuyMapper;
import com.ruoyi.develop.lvBuy.domain.LvBuy;
import com.ruoyi.develop.lvBuy.service.ILvBuyService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购单Service业务层处理
 *
 * @author qh
 * @date 2024-08-09
 */
@Service
public class LvBuyServiceImpl implements ILvBuyService {
    @Resource
    private LvBuyMapper lvBuyMapper;

    /**
     * 查询采购单
     *
     * @param id 采购单主键
     * @return 采购单
     */
    @Override
    public LvBuy selectById(String id) {
        return lvBuyMapper.selectById(id);
    }

    /**
     * 查询采购单列表
     *
     * @param entity 采购单
     * @return 采购单
     */
    @Override
    public List<LvBuy> selectList(LvBuy entity) {
        return lvBuyMapper.selectList(entity);
    }

    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<LvBuy> listScope(LvBuy entity) {
        return lvBuyMapper.listScope(entity);
    }


    @Override
    public List<LvBuy> listToMerge(LvBuy entity) {
        return lvBuyMapper.listToMerge(entity);
    }

    /**
     * 新增采购单
     *
     * @param entity 采购单
     * @return 结果
     */
    @Override
    public int insert(LvBuy entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        if(StringUtils.isBlank(entity.getNos())){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String today = sdf.format(entity.getDdDate()==null?new Date():entity.getDdDate());
            String codeMaxString = lvBuyMapper.selectMaxCode(today);
            if(StringUtils.isNotBlank(codeMaxString)){
                // 最大编号+1
                codeMaxString = Long.parseLong(codeMaxString)+1+"";
            }else{
                codeMaxString = today+"0001";
            }
            entity.setNos(codeMaxString);
        }
        entity.setDeptId(SecurityUtils.getDeptId());
        entity.setUserId(SecurityUtils.getUserId());
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return lvBuyMapper.insert(entity);
    }

    /**
     * 修改采购单
     *
     * @param entity 采购单
     * @return 结果
     */
    @Override
    public int update(LvBuy entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return lvBuyMapper.update(entity);
    }

    /**
     * 批量删除采购单
     *
     * @param ids 需要删除的采购单主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return lvBuyMapper.deleteByIds(ids);
    }

    /**
     * 删除采购单信息
     *
     * @param id 采购单主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return lvBuyMapper.deleteById(id);
    }


}
